<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\admin\model\carousel\Images;
use app\admin\model\Announcements;
use app\admin\model\Goods;
use app\common\model\Category;
use app\admin\model\giy\Evaluate;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    //获取轮播图
    public function getLbList()
    {
        $model = new Images();
        $list = $model->where('is_active', '1')->order('weigh desc')->paginate()->each(function ($item) {
            $item['image'] = cdnurl($item['image'], true);
            $item['createtime'] = date("Y-m-d H:i:s", $item['createtime']);
            $item['updatetime'] = date("Y-m-d H:i:s", $item['updatetime']);
            return $item;
        });
        $this->success(__('Operation completed'), $list);
    }

    //公告列表
    public function getGgList()
    {
        $model = new Announcements();
        $list = $model->order('createtime desc')->paginate()->each(function ($item) {
            $item['createtime'] = date("Y-m-d H:i:s", $item['createtime']);
            $item['updatetime'] = date("Y-m-d H:i:s", $item['updatetime']);
            return $item;
        });
        $this->success(__('Operation completed'), $list);
    }

    //商品列表
    public function getGoodsList()
    {
        $type = $this->request->param('type/d');//类型
        $category_id = $this->request->param('category_id/d');//分类
        $is_recommended = $this->request->param('is_recommended/d', 0);//是否推荐
        $where = [];
        $where['type'] = $type;
        if ($category_id) {
            $where['category_id'] = $category_id;
        }
        $where['is_recommended'] = $is_recommended;
        $where['is_active'] = 1;
        $model = new Goods();
        $list = $model->where($where)->order('createtime desc')->paginate()->each(function ($item) {
            $item['goods_image'] = cdnurl($item['goods_image'], true);
            $item['createtime'] = date("Y-m-d H:i:s", $item['createtime']);
            $item['updatetime'] = date("Y-m-d H:i:s", $item['updatetime']);
            $images = explode(',', $item['goods_images']);
            $data = [];
            foreach ($images as $v) {
                $image = cdnurl($v, true);
                array_push($data, $image);
            }
            $item['goods_images'] = json_encode($data);
            return $item;
        });
        $this->success(__('Operation completed'), $list);
    }

    //分类列表
    public function getCategoryList()
    {
        $model = new Category();
        $list = $model->order('weigh desc')->paginate()->each(function ($item) {
            $item['image'] = cdnurl($item['image'], true);
            $item['createtime'] = date("Y-m-d H:i:s", $item['createtime']);
            $item['updatetime'] = date("Y-m-d H:i:s", $item['updatetime']);
            return $item;
        });
        $this->success(__('Operation completed'), $list);
    }

    //提现手续费比例
    public function getSite()
    {
        $charge = config('site.handling_charge') ?? 5;
        $data = array(
            'charge' => $charge,
        );
        $this->success(__("Operation completed"), $data);
    }

    //商品详情
    public function goodsDetails()
    {
        $goods_id = $this->request->param('goods_id');
        $model = new Goods();
        $info = $model->where('goods_id', $goods_id)->find();
        if (!$info) {
            $this->error(__('No results were found'));
        }
        $images = explode(',', $info['goods_images']);
        $data = [];
        foreach ($images as $v) {
            $image = cdnurl($v, true);
            array_push($data, $image);
        }
        $info['goods_image'] = cdnurl($info['goods_image'], true);
        $info['goods_images'] = json_encode($data);
        $info['createtime'] = date("Y-m-d H:i:s", $info['createtime']);
        $info['updatetime'] = date("Y-m-d H:i:s", $info['updatetime']);
        $this->success(__("Operation completed"), $info);
    }

    //商品评论
    public function evaluate()
    {
        $goods_id = $this->request->param('goods_id');
        $model = new Evaluate();
        $where = [];
        $where['goods_id'] = $goods_id;
        $list = $model->where($where)->order('createtime desc')->paginate()->each(function ($item) {
            $item['createtime'] = date("Y-m-d H:i:s", $item['createtime']);
            $item['updatetime'] = date("Y-m-d H:i:s", $item['updatetime']);
            return $item;
        });
        $this->success(__("Operation completed"), $list);
    }
    //商品评论
    public function myEvaluate()
    {
        $user=$this->auth->getUserinfo();
        $goods_id = $this->request->param('goods_id');
        $model = new Evaluate();
        $where = [];
        $where['user_id']=$user['id'];
        $where['goods_id'] = $goods_id;
        $list = $model->where($where)->order('createtime desc')->paginate()->each(function ($item) {
            $item['createtime'] = date("Y-m-d H:i:s", $item['createtime']);
            $item['updatetime'] = date("Y-m-d H:i:s", $item['updatetime']);
            return $item;
        });
        $this->success(__("Operation completed"), $list);
    }
}
